﻿using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.ElasticSearch;
using AutoMapper.QueryableExtensions;
using FwProject.Com.Dto;
using FwProject.ShopApplication;
using FwProject.ShoppingMall.Config;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FwProject.Com
{
    public class RegionAppService : FwProjectAppServiceBase, IRegionAppService
    {
        private readonly IRepository<Region, string> _regionRepository;
        public IElasticsearch _elasticsearch;

        public RegionAppService(IElasticsearch elasticsearch, IRepository<Region, string> regionRepository)
        {
            _regionRepository = regionRepository;
            _elasticsearch = elasticsearch;
        }
        [AbpAuthorize]
        public async Task GenerateAsync()
        {
            var regions = _regionRepository.GetAll().MapTo<List<RegionDto>>();
            var data = Getregion(regions,null);

            await _elasticsearch.BulkAddorUpdateAsync<RegionDto, string>(nameof(RegionDto).ToLower(), data);
        }
        /// <summary>
        /// 获取所有的地域数据
        /// </summary>
        /// <returns></returns>
        [AbpAuthorize]
        public List<RegionDto> GetAllRegion()
        {
            var regions = _regionRepository.GetAll().MapTo<List<RegionDto>>();
            return Getregion(regions,null);

        }

        [AbpAuthorize]
        private List<RegionDto> Getregion(List<RegionDto> re,string parentId)
        {
            
            List<RegionDto> list =new List<RegionDto>();
            foreach (var item in re.Where(o=>o.ParentId==parentId))
            {
                RegionDto dto = new RegionDto();
                dto = item;
                item.Childs = new List<RegionDto>();
                item.Childs = Getregion(re,item.Id);
                list.Add(dto);
            }

            return list;
        }


        public async Task<LayerDataBaseDto> GetPagedListAsync(string parentid, string keyword)
        {
            var query = _regionRepository.GetAll().Where(o => o.ParentId == parentid);

            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(o => o.Name.Contains(keyword));

            var model = await query.ProjectTo<RegionDto>().ToListAsync();
            LayerDataBaseDto dto = new LayerDataBaseDto();
            dto.Data = model;
            return dto;
        }
        [AbpAuthorize]
        public async Task<RegionDto> GetEntityAsync(string id)
        {
            var entity = await _regionRepository.GetAsync(id);
            var dto= entity.MapTo<RegionDto>();
            dto.ParentId = string.Join(">",(await GetParentNamesAsync(dto.ParentId)));
            return dto;
        }
        [AbpAuthorize]
        public async Task<List<string>> GetParentNamesAsync(string id)
        {
            List<string> list = new List<string>();
            if (string.IsNullOrEmpty(id))
                return list;

            var region = await _regionRepository.GetAsync(id);

            if (!string.IsNullOrEmpty(region.ParentId))
            {
                var parent = await _regionRepository.GetAsync(region.ParentId);
                list.Add(parent.Name);
                list.Add(region.Name);
            }
            else
            {
                list.Add(region.Name);
            }
            return list;
        }

        [AbpAuthorize]
        public async Task<string> CreateAsync(RegionEnterDto input)
        {
            var entity = input.MapTo<Region>();
            var maxid = _regionRepository.GetAll().Max(o => o.Id);

            entity.Id = (int.Parse(maxid) + 1).ToString();

            entity.Spell = FontHelper.GetPinyin(entity.Name);
            entity.Initial = FontHelper.GetFirstPinyin(entity.Name);

            return await _regionRepository.InsertAndGetIdAsync(entity);

        }
        [AbpAuthorize]
        public async Task ModifyAsync(string id, RegionEnterDto input)
        {
            var entity = await _regionRepository.GetAsync(id);
            input.MapTo(entity);

            entity.Spell = FontHelper.GetPinyin(entity.Name);
            entity.Initial = FontHelper.GetFirstPinyin(entity.Name);

            await _regionRepository.UpdateAsync(entity);
        }
        [AbpAuthorize]
        public async Task DeleteAsync(string id)
        {
            await _regionRepository.DeleteAsync(id);
        }
    }
}
